package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;

import com.itheima.service.CheckItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 体检检查项管理
 */
@RestController
@RequestMapping("/checkitem")
@Slf4j
@Api(tags = "检查项管理")
public class CheckItemController {

    @Autowired
    private CheckItemService checkItemService;

    /**
     * 新增检查项
     * @param checkItem
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增检查项")
    public Result add(@RequestBody CheckItem checkItem){
        log.info("新增检查项：{}",checkItem);
        try {
            checkItemService.add(checkItem);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * 根据id删除检查项
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除检查项")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除检查项：{}",id);
        try {
            checkItemService.deleteById(id);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * 编辑检查项
     * @param checkItem
     * @return
     */
    @PutMapping("/edit")
    @ApiOperation("编辑检查项")
    public Result edit(@RequestBody CheckItem checkItem){
        log.info("编辑检查项：{}",checkItem);
        try {
            checkItemService.edit(checkItem);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }


    /**
     * 检查项分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    @ApiOperation("检查项分页查询")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("检查项分页查询：{}",queryPageBean);
        return checkItemService.findPage(queryPageBean);
    }

    /**
     * 查询所有检查项
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation("查询所有检查项")
    public Result findAll(){
        log.info("查询所有检查项");
        List<CheckItem> checkItemList = null;
        try {
            checkItemList = checkItemService.findAll();
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemList);
    }

    /**
     * 根据id查询检查项
     * @param id
     * @return
     */
    public Result findById(Integer id){
        log.info("根据id查询检查项：{}",id);
        try {
            CheckItem checkItem = checkItemService.findById(id);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS);
    }

    /**
     * 根据检查组id查询对应的所有检查项id
     * @param checkgroupId
     * @return
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    @ApiOperation("根据检查组id查询对应的所有检查项id")
    public Result findCheckItemIdsByCheckGroupId(Integer checkgroupId){
        try{
            List<Integer> checkitemIds =
                    checkItemService.findCheckItemIdsByCheckGroupId(checkgroupId);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkitemIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}
